// pages/device/list/index.js
var app = getApp(), fetchApi = require("../../../utils/httpPost.js")
import Toast from '../../../component/vant/toast/toast';
import Dialog from '../../../component/vant/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    merList:[],
    merNames:[],
    recordList:[],
    pageSize:10,
    pageIndex: 1,
    netWork:false,
    loading:false,
    hasData:false,
    isLoading:true,
    mer_id:0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.onHttpMerList()
  },
  changeMer(e){
    console.log('changeMer',e);
    var mer_id = e.detail;
    this.data.mer_id = mer_id;
    this.data.pageIndex = 1;
    this.onHttpInit()
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpMerList: function () {
    var that = this
    fetchApi.httpPost('/mer/myList',{
      keyword:'',
      shop_type: 1,
      pageIndex: 1,
      pageSize: 100,
    }, function (res) {
      console.log("获取我的商家列表", res)
      if(res.data.totalcount > 0){
        var mers = res.data.data.mers;
        for (var index in mers) {
          that.data.merList.splice(that.data.merList.length, 0, mers[index]);
        }
      }
      that.data.mer_id = that.data.merList.length > 0 ?  that.data.merList[0].id : 0;
      that.setData({
        merList: that.data.merList,
        merNames:res.data.data.mer_names,
        mer_id:that.data.merList.length > 0 ?  that.data.merList[0].id : 0,
      });
      that.onHttpInit()
      // complete
      wx.stopPullDownRefresh() //停止下拉刷新
    },function(){})
  },
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/mer/settlement/list', {
      pageIndex:that.data.pageIndex,
      pageSize:that.data.pageSize,
      mer_id:that.data.mer_id,
    }, function (res) {
      console.log("获取领蛋结算记录", res)
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          that.data.pageIndex = that.data.pageIndex + 1
          var records = res.data.data.records;
          if(records.length > 0){
            console.log('records',records)
            for (var index in records) {
              that.data.recordList.splice(that.data.recordList.length, 0, records[index]);
            }
            that.setData({
              hasData: true,
              recordList: that.data.recordList
            })
          }
        }
      }else{
        Toast.fail(res.data.msg)
      }
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      that.setData({ netWork: true,loading: true,  isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false,loading: true,  isLoading: false, })
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refreshdeviceList()
  },
  refreshdeviceList:function(){
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      hasData:false,
      recordList:[]
    })
    this.data.pageIndex = 1
    this.onHttpInit()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      isLoading: true
    })
    this.onHttpInit()
  },
})